home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / GraphicViewers / pCD / Source / hpcdtoppm.0.4 / postscr.c < prev    next >
C/C++ Source or Header  |  1993-03-27  |  4KB  |  158 lines

  1. /* hpcdtoppm (Hadmut's pcdtoppm) v0.4
  2. *  Copyright (c) 1992, 1993 by Hadmut Danisch (danisch@ira.uka.de).
  3. *  Permission to use and distribute this software and its
  4. *  documentation for noncommercial use and without fee is hereby granted,
  5. *  provided that the above copyright notice appear in all copies and that
  6. *  both that copyright notice and this permission notice appear in
  7. *  supporting documentation. It is not allowed to sell this software in 
  8. *  any way. This software is not public domain.
  9. */
  10.  
  11. #include "hpcdtoppm.h"
  12.  
  13.  
  14. float PAPER_LEFT   =DEF_PAPER_LEFT;
  15. float PAPER_BOTTOM =DEF_PAPER_BOTTOM;
  16. float PAPER_WIDTH  =DEF_PAPER_WIDTH;
  17. float PAPER_HEIGHT =DEF_PAPER_HEIGHT;
  18.  
  19.  
  20.  
  21. static char pshdr[]=
  22. "%%Creator: hpcdtoppm by Hadmut Danisch (danisch@ira.uka.de), hpcd_ps by Adolf Mathias (mathias@ira.uka.de)\n";
  23.  
  24.  
  25. /* find an appropriate scaling coefficient and generate a ps header
  26.  * including a BoundingBox comment and a translate/scale sequence to 
  27.  * fit the pixw*pixh image into a square on paper with PS user coordinates 
  28.  * x,y,w,h 
  29.  */
  30. static void size_dependant(pixw,pixh, x,y,w,h)
  31.   int pixw,pixh;
  32.   float x,y,w,h;
  33. { float scale=(float)w/pixw,scaley=(float)h/pixh;
  34.  
  35.   if(scale>scaley) scale=scaley;
  36.   x+=w/2.0;y+=h/2.0;
  37.   fprintf(fout,"%s",pshdr);
  38.   fprintf(fout,"%%%%BoundingBox %.8g %.8g %.8g %.8g\n",
  39.      x-scale*pixw/2.0,y-scale*pixh/2.0,
  40.      x+scale*pixw/2.0,y+scale*pixh/2.0);
  41.  
  42.   if(pcdname) fprintf(fout,"%%%%Title: %s\n",pcdname);
  43.   fputs("%%EndComments\n\n",fout);
  44.  
  45.   fprintf(fout,"%f %f translate\n",x-scale*pixw/2.0,y-scale*pixh/2.0);
  46.   fprintf(fout,"%f %f scale\n",scale*pixw,scale*pixh);
  47. }
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54. static void sub_psgrey(w,h, ptr,zeil,pix)
  55.   sdim w,h, zeil,pix;
  56.   uBYTE *ptr;
  57.  
  58. { sdim x,y;
  59.   register uBYTE *p;
  60.   int c;
  61.  
  62.   size_dependant(w,h,PAPER_LEFT,PAPER_BOTTOM,PAPER_WIDTH,PAPER_HEIGHT);
  63.   fprintf(fout,"%ld string\n",w);
  64.   fprintf(fout,"%ld %ld 8\n",w,h);    /* always 8 bit per channel */
  65.   fprintf(fout,"[%ld 0 0 %ld 0 %ld]\n",w,-h,h);
  66.   fputs("{currentfile 1 index readhexstring pop} image\n",fout);
  67.   
  68.   c=0;
  69.   for(y=0;y<h;y++,ptr+=zeil)
  70.     for(p=ptr,x=0;x<w;x++,p+=pix)
  71.       {fprintf(fout,"%.2X",*p);
  72.        if(!(++c % 36)) fputs("\n",fout);
  73.       }
  74.  
  75.   fputs("pop\n",fout);
  76. }
  77.  
  78.  
  79. void write_epsgrey(w,h, ptr,zeil,pix)
  80.   sdim w,h, zeil,pix;
  81.   uBYTE *ptr;
  82.   fputs("%!PS-Adobe-2.0 EPSF-2.0\n",fout);
  83.   sub_psgrey(w,h, ptr,zeil,pix);
  84. }
  85.  
  86. void write_psgrey(w,h, ptr,zeil,pix)
  87.   sdim w,h, zeil,pix;
  88.   uBYTE *ptr;
  89.   fputs("%!PS-Adobe-2.0\n",fout);
  90.   sub_psgrey(w,h, ptr,zeil,pix);
  91.   fputs("showpage\n",fout);
  92. }
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104. static void sub_psrgb(w,h, rptr,rzeil,rpix,  
  105.                gptr,gzeil,gpix,  
  106.                bptr,bzeil,bpix) 
  107.   sdim w,h, rzeil,gzeil,bzeil, rpix,gpix,bpix;
  108.   uBYTE *rptr,*gptr,*bptr;
  109.  
  110. { sdim x,y;
  111.   register uBYTE *pr,*pg,*pb;
  112.   int c;
  113.  
  114.   size_dependant(w,h,PAPER_LEFT,PAPER_BOTTOM,PAPER_WIDTH,PAPER_HEIGHT);
  115.   fprintf(fout,"%ld string\n",w*3);
  116.   fprintf(fout,"%ld %ld 8\n",w,h);    /* always 8 bit per channel */
  117.   fprintf(fout,"[%ld 0 0 %ld 0 %ld]\n",w,-h,h);
  118.   fputs("{currentfile 1 index readhexstring pop} false 3 colorimage\n",fout);
  119.   
  120.   c=0; 
  121.   for(y=0;y<h;y++,rptr+=rzeil,gptr+=gzeil,bptr+=bzeil)
  122.     for(pr=rptr,pg=gptr,pb=bptr,x=0;x<w;x++,pr+=rpix,pg+=gpix,pb+=bpix)
  123.       {fprintf(fout,"%.2X%.2X%.2X",*pr,*pg,*pb);
  124.        if(!(++c % 12)) fputs("\n",fout);
  125.       }
  126.  
  127.   fputs("\npop\n",fout);
  128. }
  129.  
  130.  
  131. void write_epsrgb(w,h, rptr,rzeil,rpix,  
  132.                  gptr,gzeil,gpix,  
  133.                  bptr,bzeil,bpix) 
  134.   sdim w,h, rzeil,gzeil,bzeil, rpix,gpix,bpix;
  135.   uBYTE *rptr,*gptr,*bptr;
  136.   fputs("%!PS-Adobe-2.0 EPSF-2.0\n",fout);
  137.   sub_psrgb(w,h, rptr,rzeil,rpix,gptr,gzeil,gpix,bptr,bzeil,bpix);
  138. }
  139.  
  140.  
  141.  
  142. void write_psrgb(w,h, rptr,rzeil,rpix,  
  143.                  gptr,gzeil,gpix,  
  144.                  bptr,bzeil,bpix) 
  145.   sdim w,h, rzeil,gzeil,bzeil, rpix,gpix,bpix;
  146.   uBYTE *rptr,*gptr,*bptr;
  147.   fputs("%!PS-Adobe-2.0\n",fout);
  148.   sub_psrgb(w,h, rptr,rzeil,rpix,gptr,gzeil,gpix,bptr,bzeil,bpix);
  149.   fputs("showpage\n",fout);
  150. }
  151.  
  152.  
  153.  
  154.